package com.kangyi.admin.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kangyi.admin.mapper.ActivitiesSignupMapper;
import com.kangyi.admin.mapper.AdminUserMapper;
import com.kangyi.admin.service.ActivitiesSignupService;
import com.kangyi.constant.MessageConstant;
import com.kangyi.context.BaseContext;
import com.kangyi.dto.ActivitiesSignupPageQueryDTO;
import com.kangyi.dto.BatchProcessingDTO;
import com.kangyi.entity.ActivitySignup;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.exception.PermissionsException;
import com.kangyi.exception.ToExamineException;
import com.kangyi.result.ActivitiesSignupPageResult;
import com.kangyi.vo.ActivitiesSignupPageQueryVO;
import com.kangyi.vo.AdminUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class ActivitiesSignupServiceImpl implements ActivitiesSignupService {

    @Autowired
    private ActivitiesSignupMapper activitiesSignupMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;


    //调用后检查当前线程id是否有足够的权限进行后续操作
    public void validateUserPermissions() {
        Integer currentId = BaseContext.getCurrentId();
        AdminUserVO adminUserVO = adminUserMapper.selectById(currentId);
        if (!adminUserVO.getPermissions().equals(1)) {
            throw new PermissionsException(MessageConstant.INSUFFICIENT_PRIVILEGE);
        }
    }


    @Override
    public ActivitiesSignupPageResult pageQuery(ActivitiesSignupPageQueryDTO activitiesSignupPageQueryDTO) {
        validateUserPermissions();
        LocalDateTime begin = null;
        LocalDateTime end = null;
        if (activitiesSignupPageQueryDTO.getStartTime() != null) {
            LocalDate startTime = activitiesSignupPageQueryDTO.getStartTime();
            begin = LocalDateTime.of(startTime, LocalTime.MIN);
            end = LocalDateTime.of(startTime, LocalTime.MAX);
        }
        PageHelper.startPage(activitiesSignupPageQueryDTO.getPage(),activitiesSignupPageQueryDTO.getPageSize());
        Page<ActivitiesSignupPageQueryVO> page = activitiesSignupMapper.pageQuery(activitiesSignupPageQueryDTO,begin,end);
        long total = page.getTotal();
        List<ActivitiesSignupPageQueryVO> result = page.getResult();
        int pendingReview = 0;
        int Passed = 0;
        int rejected = 0;
        for (ActivitiesSignupPageQueryVO vo : result) {
            if (vo.getStatus() == 1) {
                pendingReview++;
            } else if (vo.getStatus() == 2) {
                Passed++;
            } else if (vo.getStatus() == 3) {
                rejected++;
            }
        }
        return new ActivitiesSignupPageResult(total,result,pendingReview,Passed,rejected);
    }

    @Override
    public void deleteActivitySignup(List<Integer> ids) {
        validateUserPermissions();
        for (Integer id : ids) {
            ActivitySignup activitySignup = activitiesSignupMapper.getById(id);
            if (activitySignup.getStatus().equals(2)) {
                throw new ToExamineException(MessageConstant.CANNOT_REFUSE_AFTER_AGREEMENT);
            }
        }

        activitiesSignupMapper.deleteActivitySignup(ids);
    }

    @Override
    public void batchProcessing(BatchProcessingDTO batchProcessingDTO) {
        validateUserPermissions();

        List<Integer> ids = batchProcessingDTO.getIds();
        for (Integer id : ids) {
            ActivitySignup activitySignup = activitiesSignupMapper.getById(id);
            if (activitySignup == null) {
                throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
            }
            if (activitySignup.getStatus().equals(2)) {
                throw new ToExamineException(MessageConstant.CANNOT_REFUSE_AFTER_AGREEING);
            }

            ActivitySignup build = ActivitySignup.builder()
                    .id(id)
                    .status(batchProcessingDTO.getType())
                    .build();
            activitiesSignupMapper.modifyStatus(build);
        }

    }
}
